package com.server.http.handler.impl;

import com.server.conditionenum.HttpReturnStatus;
import com.server.db.DataBaseManager;
import com.server.http.handler.HttpHandler;
import com.server.http.request.HttpRequestMessage;
import com.server.http.response.HttpResponseMessage;
import com.server.mine.net.HttpProtocol;
import com.server.tools.EncryptionTools;

/**
* @author	李小布
* @email	397554852@qq.com
* @version	创建时间：2018年9月20日 下午3:03:47
*/
public class ChangeAccountPassword extends HttpHandler{
	@Override
	public void handle(HttpRequestMessage request, HttpResponseMessage response) {
		int status = 0;
		String failMsg = null;
		
		String uname = request.getParameter("uname");
		String oldPassword = request.getParameter("oldPassword");
		String newPassword = request.getParameter("newPassword");
		if(uname == "" || oldPassword == "" || newPassword == "") {
			failMsg = "参数错误或参数值为空！";
		}else {
			try {
				String dbPassword = DataBaseManager.GetInstance().GetAccountPassword(uname);
				if(dbPassword != null) {
					if(dbPassword.equals(EncryptionTools.MD5(oldPassword))) {
						if(DataBaseManager.GetInstance().ChangeAccountPassword(uname, EncryptionTools.MD5(newPassword))) {
							status = 1;
						}else {
							failMsg = "未知错误！";
						}
					}else {
						failMsg = "原始密码错误！";
					}
				}else {
					failMsg = "用户不存在！";
				}
			} catch (Exception e) {
				e.printStackTrace();
				failMsg = e.getMessage();
			}
		}
		
		if(status == 1) {
			SetReturnData(response,"修改成功",HttpProtocol.CHANGEACCOUNTPASSWORD,HttpReturnStatus.Successed);
		}else {
			SetReturnData(response,failMsg,HttpProtocol.CHANGEACCOUNTPASSWORD,HttpReturnStatus.Failed);
		}
	}

}